### Table 4 Harris
### 8 coefficients of interest
  #1. Predicting Quitting: Relative vs. Attributable (Relative Coef: 18.4)
  #2. Predicting Quitting: Relative vs. Attributable (Attributable Coef: 1.6 )
  #3. Predicting Quitting: Relative vs. Absolute (Relative Coef: 18.0)
  #4. Predicting Quitting: Relative vs. Absolute (Absolute Coef: -1.9)
  #5. Predicting Desire to Quit: Relative vs. Attributable (Relative Coef: 11.5 )
  #6. Predicting Desire to Quit: Relative vs. Attributable (Attributable Coef: 2.6)
  #7. Predicting Desire to Quit: Relative vs. Absolute (Relative Coef: 11.9)
  #8. Predicting Desire to Quit: Relative vs. Absolute (Absolute Coef: -7.2)


rm(list = ls())
#setwd("")
library(foreign)
smoking <- read.dta("harrisxx2.dta")
library(gam)

#1. Predicting Quitting: Relative vs. Attributable (Relative Coef: 18.4)
  smoking.gam1 <- gam(currsmok1 ~ s(rd) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  smoking2 <- smoking
  smoking2$rr <- wtd.quantile(smoking2$rr, weights=smoking$weight1, .25, na.rm=TRUE)
  smoking3 <- smoking
  smoking3$rr <- wtd.quantile(smoking3$rr, weights=smoking$weight1, .75, na.rm=TRUE)
  mean(predict(smoking.gam1, newdata=smoking2, type="response") - predict(smoking.gam1, newdata=smoking3, type="response"), na.rm=T)

  
#2. Predicting Quitting: Relative vs. Attributable (Attributable Coef: 1.6 )
  smoking4 <- smoking
  smoking4$rd <- quantile(smoking$rd, .25, na.rm=T)
  smoking5 <- smoking
  smoking5$rd <- quantile(smoking$rd, .75, na.rm=T)
  mean(predict(smoking.gam1, newdata=smoking4, type="response") - predict(smoking.gam1, newdata=smoking5, type="response"), na.rm=T)

  
#  Predicting Quitting: LR Test -- Relative vs. Attributable
  smoking.gam1 <- gam(currsmok1 ~ s(rd) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1a <- gam(currsmok1 ~ s(rd),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1b <- gam(currsmok1 ~ s(rr),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  anova(smoking.gam1a, smoking.gam1)
  anova(smoking.gam1b, smoking.gam1)
  

#3. Predicting Quitting: Relative vs. Absolute (Relative Coef: 18.0)
  smoking.gam1 <- gam(currsmok1 ~ s(q2062_) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  smoking2 <- smoking
  smoking2$rr <- wtd.quantile(smoking2$rr, weights=smoking$weight1, .25, na.rm=TRUE)
  smoking3 <- smoking
  smoking3$rr <- wtd.quantile(smoking3$rr, weights=smoking$weight1, .75, na.rm=TRUE)
  mean(predict(smoking.gam1, newdata=smoking2, type="response") - predict(smoking.gam1, newdata=smoking3, type="response"), na.rm=T)

    
#4. Predicting Quitting: Relative vs. Absolute (Absolute Coef: -1.9)
  smoking4 <- smoking
  smoking4$q2062_ <- quantile(smoking$q2062_, .25, na.rm=T)
  smoking5 <- smoking
  smoking5$q2062_ <- quantile(smoking$q2062_, .75, na.rm=T)
  mean(predict(smoking.gam1, newdata=smoking4, type="response") - predict(smoking.gam1, newdata=smoking5, type="response"), na.rm=T)


#  Predicting Quitting: LR Test -- Relative vs. Absolute
  smoking.gam1 <- gam(currsmok1 ~ s(q2062_) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1a <- gam(currsmok1 ~ s(q2062_),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1b <- gam(currsmok1 ~ s(rr),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  anova(smoking.gam1a, smoking.gam1)
  anova(smoking.gam1b, smoking.gam1)
  
  
#5. Predicting Desire to Quit: Relative vs. Attributable (Relative Coef: 11.5 )
  smoking.gam1 <- gam(dquit1 ~ s(rd) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  smoking2 <- smoking
  smoking2$rr <- wtd.quantile(smoking2$rr, weights=smoking$weight1, .25, na.rm=TRUE)
  smoking3 <- smoking
  smoking3$rr <- wtd.quantile(smoking3$rr, weights=smoking$weight1, .75, na.rm=TRUE)
  mean(predict(smoking.gam1, newdata=smoking3, type="response") - predict(smoking.gam1, newdata=smoking2, type="response"), na.rm=T)

  
#6. Predicting Desire to Quit: Relative vs. Attributable (Attributable Coef: 2.6)
  smoking4 <- smoking
  smoking4$rd <- quantile(smoking$rd, .25, na.rm=T)
  smoking5 <- smoking
  smoking5$rd <- quantile(smoking$rd, .75, na.rm=T)
  mean(predict(smoking.gam1, newdata=smoking5, type="response") - predict(smoking.gam1, newdata=smoking4, type="response"), na.rm=T)

  
#  Predicting Quitting: LR Test -- Relative vs. Attributable
  smoking.gam1 <- gam(dquit1 ~ s(rd) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1a <- gam(dquit1 ~ s(rd),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1b <- gam(dquit1 ~ s(rr),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(),weights=weight1)
  
  anova(smoking.gam1a, smoking.gam1)
  anova(smoking.gam1b, smoking.gam1)
  
  
#7. Predicting Desire to Quit: Relative vs. Absolute (Relative Coef: 11.9)
  smoking.gam1 <- gam(dquit1 ~ s(q2062_) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  smoking2 <- smoking
  smoking2$rr <- wtd.quantile(smoking2$rr, weights=smoking$weight1, .25, na.rm=TRUE)
  smoking3 <- smoking
  smoking3$rr <- wtd.quantile(smoking3$rr, weights=smoking$weight1, .75, na.rm=TRUE)
  mean(predict(smoking.gam1, newdata=smoking3, type="response") - predict(smoking.gam1, newdata=smoking2, type="response"), na.rm=T)
  
  
#8. Predicting Desire to Quit: Relative vs. Absolute (Absolute Coef: 7.2)
  smoking4 <- smoking
  smoking4$q2062_ <- wtd.quantile(smoking4$q2062_, weights=smoking$weight1, .25, na.rm=TRUE)
  smoking5 <- smoking
  smoking5$q2062_ <- wtd.quantile(smoking5$q2062_, weights=smoking$weight1, .75, na.rm=TRUE)
  mean(predict(smoking.gam1, newdata=smoking5, type="response") - predict(smoking.gam1, newdata=smoking4, type="response"), na.rm=T)


#  Predicting Quitting: LR Test -- Relative vs. Absolute
  smoking.gam1 <- gam(dquit1 ~ s(q2062_) + s(rr),
                      subset = control==1&rd>-500,
                      data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1a <- gam(dquit1 ~ s(q2062_),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  smoking.gam1b <- gam(dquit1 ~ s(rr),
                       subset = control==1&rd>-500,
                       data=smoking, family=gaussian(), weights=weight1)
  
  anova(smoking.gam1a, smoking.gam1)
  anova(smoking.gam1b, smoking.gam1)
